home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / UPC12BS1.ZIP / UUCICO / ULIBNMP.C < prev    next >
C/C++ Source or Header  |  1993-10-03  |  21KB  |  632 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    u l i b n m p . c                                               */
  3. /*                                                                    */
  4. /*    OS/2 named pipe support for UUCICO                              */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*    Changes Copyright (c) 1989-1993 by Kendra Electronic            */
  9. /*    Wonderworks.                                                    */
  10. /*                                                                    */
  11. /*    All rights reserved except those explicitly granted by the      */
  12. /*    UUPC/extended license agreement.                                */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *       $Id: ulibnmp.c 1.7 1993/10/03 22:09:09 ahd Exp $
  21.  *       $Log: ulibnmp.c $
  22.  * Revision 1.7  1993/10/03  22:09:09  ahd
  23.  * Use unsigned long to display speed
  24.  *
  25.  * Revision 1.6  1993/10/03  20:37:34  ahd
  26.  * Delete redundant error messages
  27.  *
  28.  * Revision 1.5  1993/09/29  04:49:20  ahd
  29.  * Delete obsolete variable
  30.  *
  31.  * Revision 1.4  1993/09/27  00:45:20  ahd
  32.  * OS/2 16 bit support
  33.  *
  34.  * Revision 1.3  1993/09/25  03:07:56  ahd
  35.  * Various small bug fixes
  36.  *
  37.  * Revision 1.2  1993/09/24  03:43:27  ahd
  38.  * General bug fixes to make work
  39.  *
  40.  * Revision 1.1  1993/09/23  03:26:51  ahd
  41.  * Initial revision
  42.  *
  43.  *
  44.  */
  45.  
  46. /*--------------------------------------------------------------------*/
  47. /*                        System include files                        */
  48. /*--------------------------------------------------------------------*/
  49.  
  50. #include <stdlib.h>
  51. #include <stdio.h>
  52. #include <string.h>
  53. #include <fcntl.h>
  54. #include <io.h>
  55. #include <time.h>
  56.  
  57. /*--------------------------------------------------------------------*/
  58. /*                         OS/2 include files                         */
  59. /*--------------------------------------------------------------------*/
  60.  
  61. #define INCL_DOSDEVIOCTL
  62. #define INCL_BASE
  63. #include <os2.h>
  64. #include <limits.h>
  65.  
  66. #ifndef __OS2__
  67. typedef USHORT APIRET ;  // Define older API return type
  68. #endif
  69.  
  70. /*--------------------------------------------------------------------*/
  71. /*                    UUPC/extended include files                     */
  72. /*--------------------------------------------------------------------*/
  73.  
  74. #include "lib.h"
  75. #include "ulibnmp.h"
  76. #include "ssleep.h"
  77. #include "catcher.h"
  78.  
  79. #include "commlib.h"
  80. #include "pos2err.h"
  81.  
  82. /*--------------------------------------------------------------------*/
  83. /*                          Global variables                          */
  84. /*--------------------------------------------------------------------*/
  85.  
  86. currentfile();
  87.  
  88. static boolean   carrierDetect = FALSE;  /* Modem is not connected     */
  89.  
  90. static boolean hangupNeeded = FALSE;
  91.  
  92. static BPS currentSpeed = 0;
  93.  
  94. static boolean passive;
  95.  
  96. #define FAR_NULL ((PVOID) 0L)
  97.  
  98. /*--------------------------------------------------------------------*/
  99. /*           Definitions of control structures for DOS API            */
  100. /*--------------------------------------------------------------------*/
  101.  
  102. static HPIPE pipeHandle;
  103.  
  104. static USHORT writeWait = 200;
  105. static USHORT readWait = 50;
  106.  
  107. static int writes, reads, writeSpins, readSpins;
  108.  
  109. /*--------------------------------------------------------------------*/
  110. /*    p p a s s i v e o p e n l i n e                                 */
  111. /*                                                                    */
  112. /*    Open a server pipe connection                                   */
  113. /*--------------------------------------------------------------------*/
  114.  
  115. #ifdef __TURBOC__
  116. #pragma argsused
  117. #endif
  118.  
  119. int ppassiveopenline(char *name, BPS baud, const boolean direct )
  120. {
  121.  
  122.    APIRET rc;
  123.  
  124.    if (portActive)                  /* Was the port already active?    */
  125.       closeline();                  /* Yes --> Shutdown it before open */
  126.  
  127. #ifdef UDEBUG
  128.    printmsg(15, "ppassiveopenline: %s, %d", name, baud);
  129. #endif
  130.  
  131. /*--------------------------------------------------------------------*/
  132. /*                          Perform the open                          */
  133. /*--------------------------------------------------------------------*/
  134.  
  135. #ifdef __OS2__
  136.    rc =  DosCreateNPipe( (PSZ) "\\pipe\\uucp",
  137.                          &pipeHandle,
  138.                          NP_ACCESS_DUPLEX | NP_INHERIT | NP_NOWRITEBEHIND,
  139.                          NP_NOWAIT | 1,
  140.                          MAXPACK,
  141.                          MAXPACK,
  142.                          30000 );
  143. #else
  144.    rc =  DosMakeNmPipe(  (PSZ) "\\pipe\\uucp",
  145.                          &pipeHandle,
  146.                          NP_ACCESS_DUPLEX | NP_INHERIT | NP_NOWRITEBEHIND,
  147.                          NP_NOWAIT | 1,
  148.                          MAXPACK,
  149.                          MAXPACK,
  150.                          30000 );
  151. #endif
  152.  
  153. /*--------------------------------------------------------------------*/
  154. /*    Check the open worked.  We translation the common obvious       */
  155. /*    error of file in use to english, for all other errors are we    */
  156. /*    report the raw error code.                                      */
  157. /*--------------------------------------------------------------------*/
  158.  
  159.    if ( rc == ERROR_SHARING_VIOLATION)
  160.    {
  161.       printmsg(0,"Pipe %s already in use", name);
  162.       return TRUE;
  163.    }
  164.    else if ( rc )
  165.    {
  166.       printOS2error("DosCreateNPipe", rc );
  167.       return TRUE;
  168.    }
  169.  
  170. /*--------------------------------------------------------------------*/
  171. /*                           Set baud rate                            */
  172. /*--------------------------------------------------------------------*/
  173.  
  174.    SIOSpeed(baud);         // Just any old large number.
  175.  
  176.    traceStart( name );     // Enable logging
  177.  
  178.    portActive = TRUE;      /* Record status for error handler        */
  179.    passive    = TRUE;
  180.    carrierDetect = FALSE;  /* Modem is not connected                 */
  181.  
  182.    return 0;
  183.  
  184. } /* ppassiveopenline */
  185.  
  186. /*--------------------------------------------------------------------*/
  187. /*       p W a i t F o r N e t C o n n e c t                          */
  188. /*                                                                    */
  189. /*       Wait for network connection                                  */
  190. /*--------------------------------------------------------------------*/
  191.  
  192. boolean pWaitForNetConnect(int timeout)
  193. {
  194.  
  195.    time_t stop;
  196.  
  197.    stop  = time( NULL ) + timeout;
  198.  
  199.    do {
  200.  
  201. #ifdef __OS2__
  202.       APIRET rc = DosConnectNPipe( pipeHandle );
  203. #else
  204.       APIRET rc = DosConnectNmPipe( pipeHandle );
  205. #endif
  206.  
  207.       if ( rc == 0 )
  208.       {
  209.          hangupNeeded = TRUE;      /* Flag that the pipe is now dirty  */
  210.          return TRUE;
  211.       }
  212.       else if ( rc == ERROR_PIPE_NOT_CONNECTED )
  213.          ssleep(5);
  214.       else {
  215.          printOS2error("DosConnectNPipe", rc );
  216.          return FALSE;
  217.       } /* else */
  218.  
  219.    } while( (stop > time( NULL )) && ! terminate_processing );
  220.  
  221.    return FALSE;
  222.  
  223. }  /* pWaitForNetConnect */
  224.  
  225. /*--------------------------------------------------------------------*/
  226. /*    p a c t i v e o p e n l i n e                                   */
  227. /*                                                                    */
  228. /*    Open a client pipe connection                                   */
  229. /*--------------------------------------------------------------------*/
  230.  
  231. #ifdef __TURBOC__
  232. #pragma argsused
  233. #endif
  234.  
  235. int pactiveopenline(char *name, BPS baud, const boolean direct )
  236. {
  237.  
  238.    APIRET rc;
  239.  
  240. #ifdef __OS2__
  241.    ULONG action;
  242. #else
  243.    USHORT action;
  244. #endif
  245.  
  246.    if (portActive)                  /* Was the port already active?     */
  247.       closeline();                  /* Yes -->